using System;
using System.Collections;
using System.Collections.Generic;
using BitMango.Diagnostics;
using UnityEngine;

namespace BitMango.SocialAPI
{
    public static class Boaster
    {
        public static void FacebookReadyToPublish(Action<bool> callback)
        {
            TaskManager.StartCoroutine(Boaster.coFacebookReadyForPublish(callback));
        }

        public static void FacebookPost(string message, string pictureUrl, Action<bool> callback = null)
        {
            TaskManager.StartCoroutine(Boaster.coFacebookPostMessage(message, pictureUrl, callback));
        }

        public static void FacebookPostImage(Texture2D img, string message, Action<bool> callback = null)
        {
            if (img == null)
            {
                UnityEngine.Debug.LogError("FacebookPostImage Fail : image is null");
                Platform.SafeCallback<bool>(callback, false);
                return;
            }
            TaskManager.StartCoroutine(Boaster.coFacebookPostImage(message, img, callback));
        }

        public static void FacebookLikePage(string url, Action<bool> callback = null)
        {
            TaskManager.StartCoroutine(Boaster.coFacebookLikePage(url, callback));
        }

        public static void Twit(string message, Action<bool> callback = null)
        {
        }

        private static bool isPermissionExist(string perm)
        {
            return Boaster.permissions != null && Boaster.permissions.Contains(perm);
        }

        private static void buildPermissionList()
        {
        }

        private static IEnumerator coFacebookLogin()
        {
            yield break;
        }

        private static IEnumerator coFacebookReadyForPublish(Action<bool> callback = null)
        {
            if (Boaster.fbInit)
            {
                Platform.SafeCallback<bool>(callback, Boaster.fbInit);
                yield break;
            }
            Boaster.fbCancel = false;
            Boaster.fbDone = false;
            yield return Platform.Info.StartCoroutine(Boaster.coFacebookLogin());
            Boaster.fbInit = true;
            Platform.SafeCallback<bool>(callback, Boaster.fbInit);
            yield break;
        }


        private static IEnumerator coFacebookLikePage(string url, Action<bool> callback)
        {
            yield return Platform.Info.StartCoroutine(Boaster.coFacebookReadyForPublish(null));
            if (!Boaster.fbInit)
            {
                Platform.SafeCallback<bool>(callback, Boaster.fbInit);
                yield break;
            }
            bool fbCancel = false;
            bool fbDone = false;
            Platform.SafeCallback<bool>(callback, fbDone);
            yield break;
        }

        private static IEnumerator coFacebookPostMessage(string message, string pictureUrl, Action<bool> callback)
        {
            yield return Platform.Info.StartCoroutine(Boaster.coFacebookReadyForPublish(null));
            if (!Boaster.fbInit)
            {
                Platform.SafeCallback<bool>(callback, Boaster.fbInit);
                yield break;
            }
            Boaster.fbCancel = false;
            Boaster.fbDone = false;
            Platform.SetRecoverPoint("fb");
            while (!Boaster.fbDone && !Boaster.fbCancel)
            {
                yield return 0;
            }
            Platform.SafeCallback<bool>(callback, !Boaster.fbCancel);
            yield break;
        }

        private static void facebookUploadImage(string message, Texture2D image, Action<string> callback)
        {
            byte[] contents = image.EncodeToPNG();
            WWWForm wwwform = new WWWForm();
            wwwform.AddBinaryData("image", contents, image.name + ".png");
            wwwform.AddField("message", message);
        }

        private static IEnumerator coFacebookPostImage(string message, Texture2D image, Action<bool> callback)
        {
            yield return Platform.Info.StartCoroutine(Boaster.coFacebookReadyForPublish(null));
            if (!Boaster.fbInit)
            {
                Platform.SafeCallback<bool>(callback, Boaster.fbInit);
                yield break;
            }
            string imageID = string.Empty;
            if (image != null)
            {
                Boaster.fbCancel = false;
                Boaster.fbDone = false;
                Boaster.facebookUploadImage(message, image, delegate (string _imageID)
                {
                    UnityEngine.Debug.Log("_imageID  : " + _imageID);
                    if (_imageID != string.Empty)
                    {
                        imageID = _imageID;
                        Boaster.fbCancel = false;
                        Boaster.fbDone = true;
                    }
                    else
                    {
                        Boaster.fbDone = false;
                        Boaster.fbCancel = true;
                    }
                });
                while (!Boaster.fbDone && !Boaster.fbCancel)
                {
                    yield return 0;
                }
            }
            else
            {
                Boaster.fbCancel = false;
                Boaster.fbDone = false;
                string imageLink = string.Empty;
                Platform.SetRecoverPoint("fb");
                while (!Boaster.fbDone && !Boaster.fbCancel)
                {
                    yield return 0;
                }
            }
            Platform.SafeCallback<bool>(callback, Boaster.fbDone);
            yield break;
        }

        private static void facebookLoginFailed(string error)
        {
            UnityEngine.Debug.Log("Facebook login failed: " + error);
            Boaster.fbCancel = true;
        }

        private static void facebookReauthorizationFailed(string error)
        {
            UnityEngine.Debug.Log("facebookReauthorizationFailed : " + error);
            Boaster.fbCancel = true;
        }

        private static void facebookDialogFailed(string error)
        {
            UnityEngine.Debug.Log("facebookDialogFailed: " + error);
            Boaster.fbCancel = true;
        }

        private static void facebookComposerCompleted(bool didSucceed)
        {
            UnityEngine.Debug.Log("facebookComposerCompletedEvent did succeed: " + didSucceed);
            if (didSucceed)
            {
                Boaster.fbDone = true;
            }
            else
            {
                Boaster.fbCancel = true;
            }
        }

        private static void facebookDialogCompletedWithUrl(string url)
        {
            UnityEngine.Debug.Log("facebookDialogCompletedWithUrl: " + url);
            Boaster.fbDone = true;
        }

        private static void facebookGraphRequestCompleted(object json)
        {
            UnityEngine.Debug.Log("facebookGraphRequestCompleted succeed: " + json);
            Boaster.fbDone = true;
        }

        private static void facebookGraphRequestFailed(string error)
        {
            UnityEngine.Debug.Log("facebookGraphRequestFailed failed : " + error);
            Boaster.fbCancel = true;
        }

        private static void twitterLoginFailed(string err)
        {
            UnityEngine.Debug.Log("twitterLoginFailed : " + err);
            Boaster.twCancel = true;
        }

        private static void twitterPostFailed(string err)
        {
            UnityEngine.Debug.Log("twitterPostFailed : " + err);
            Boaster.twCancel = true;
        }

        private static void twitterRequestSucceeded(object obj)
        {
            Boaster.twDone = true;
        }

        private static void twitterPostSucceeded()
        {
            Boaster.twDone = true;
        }

        private static void twitterSheetCompleted(bool success)
        {
            if (success)
            {
                Boaster.twDone = true;
            }
            else
            {
                Boaster.twCancel = true;
            }
        }

        private static bool fbInit;

        private static bool fbCancel;

        private static bool fbDone;

        private static List<string> permissions;

        private static bool twInit;

        private static bool twCancel;

        private static bool twDone;
    }
}
